﻿using IFix.Core;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using UnityEngine;

public class InjectFixManager 
{
    private static InjectFixManager ins;
    public static InjectFixManager Ins
    {
        get
        {
            if(ins == null)
            {
                VirtualMachine.Info = (s) => UnityEngine.Debug.Log(s);
                ins = new InjectFixManager();
            }
            return ins;
        }
    }

    public void LoadPatchBytes(int type,byte[] patchBytes)
    {
        UnityEngine.Debug.Log(string.Format( "patchBytesSize:{0} type:{1} , loading Assembly.patch ...", patchBytes.Length, type));
        //string path;
        //if (type == (int)DllType.CSharpDll)
        //    path = @"C:\WORK\InjectFix\Source\UnityProj\Assembly-CSharp.patchEx.bytes";
        //else
        //    path = @"C:\WORK\InjectFix\Source\UnityProj\Assembly-CSharp-firstpass.patch.bytes";

        //if (File.Exists(path))
        //    File.Delete(path);

        //var writer = File.Open(path, FileMode.OpenOrCreate);
        //writer.Write(patchBytes, 0, patchBytes.Length);
        //writer.Close();
        //return;

        var sw = Stopwatch.StartNew();
        PatchManager.Load(new MemoryStream(patchBytes));
        UnityEngine.Debug.Log("patch Assembly-CSharp.patch, using " + sw.ElapsedMilliseconds + " ms");
    }
}
